<template>

  <el-container class="home_container">
    <!-- 头部 -->
    <el-header>
      <div>
        <img src="../assets/logo.png">
        <span>电子商城</span>
      </div>
      <el-button
        type="info"
        @click="logout"
      >退出</el-button>
    </el-header>
    <!-- 主体 -->
    <el-container>
      <!-- 侧边 -->
      <el-aside width="200px">

        <!-- 侧边栏菜单 -->
        <el-menu
          @open="handleOpen"
          @close="handleClose"
          background-color="#333744"
          text-color="#fff"
          actuve-text-color="#4235007"
          unique-opened
          router
          :default-active="NavPath"
        >
          <!--一级菜单 -->
          <el-submenu
            :index="item.id+''"
            v-for="item in MenusList"
            :key="item.id"
          >
            <template slot="title">
              <!-- 图标 -->
              <i :class="obj[item.id]"></i>
              <!--data是本地的可以直接拿来用  通过id实现!-->
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单    el-menu-item才能使用跳转功能-->
            <el-menu-item
              :index="'/'+item2.path"
              v-for="item2 in item.children"
              :key="item2.id"
              @click="StorePath('/'+item2.path)"
            >
              <i class="el-icon-location"></i>{{item2.authName}}
            </el-menu-item>
          </el-submenu>
        </el-menu>

      </el-aside>
      <!-- 内容 -->
      <el-main>
        <router-view></router-view>
      </el-main>

    </el-container>
  </el-container>

</template>

<script>
export default {
  created() {
    this.getMenusList()
    this.NavPath = window.sessionStorage.getItem('NavPath')
  },
  data() {
    return {
      MenusList: [],
      obj: {
        //数组来的
        125: 'el-icon-s-custom',
        103: 'el-icon-s-grid',
        101: 'el-icon-shopping-bag-1',
        102: 'el-icon-s-order',
        145: 'el-icon-s-marketing',
      },
      NavPath: '',
    }
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    handleOpen(key, keyPath) {
      console.log(key, keyPath)
    },
    handleClose(key, keyPath) {
      console.log(key, keyPath)
    },
    //获取所有菜单
    async getMenusList() {
      const { data: res } = await this.$http.get('menus')
      console.log(res)
      if (res.meta.status != 200) return this.$message.error(res.data.msg)
      this.MenusList = res.data
    },
    //保存连接的激活状态
    StorePath(NavPath) {
      window.sessionStorage.setItem('NavPath', NavPath)
    },
  },
}
</script>

<style lang="less" scoped>
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20 px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
      justify-content: right;
    }
  }
  img {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background-color: #eee;
  }
  el-button {
  }
} //标签可以当类名使用
.el-aside {
  background-color: #333744;
}
.el-main {
  background-color: #eaedf1;
}
.home_container {
  height: 100%;
}
.el-menu {
  border-right: 0;
}
.el-menu {
  border-right: 0;
}
</style>
